package com.peace.hospital.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import com.peace.hospital.entity.Dto;
import com.peace.hospital.entity.common.Department;


import com.peace.hospital.entity.common.ResponseBean;
import com.peace.hospital.entity.common.Staffinfo;
import com.peace.hospital.entity.system.Position;
import com.peace.hospital.service.IStaffinfoService;

import org.springframework.beans.factory.annotation.Autowired;;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * <p>
 * 医院员工信息表 前端控制器
 * </p>
 *
 * @author xie
 * @since 2019-11-12
 */
@RestController
@RequestMapping("/staffinfo")
public class StaffinfoController {
//    @Autowired
//    private RedisTemplate redisTemplate;


//    @Autowired
//    private  Staffinfo staffinfo;

    @Autowired
    private IStaffinfoService iStaffinfoService;


    @InitBinder("Staffinfo")
    public void initBinderCourse(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("Staffinfo.");
    }

    @InitBinder("Position")
    public void initBinderPosition(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("Position.");
    }


    @InitBinder("Department")
    public void initBinderDepartment(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("Department.");
    }

    //首页查询用户
    @RequestMapping(value = "/select", method = {RequestMethod.GET})
    public String SelestStaffinfo() {
        List<Staffinfo> staffinfoList = iStaffinfoService.SelectStaffinfo();
        System.out.println(staffinfoList+"**********************************************");

        if(staffinfoList!=null){
            /**
             *    当对集合里面装有集合时加上
             *    SerializerFeature.DisableCircularReferenceDetect就是禁止循环引用的方案
             */
           String s = JSON.toJSONString(new ResponseBean(200,"SUCCESS",staffinfoList), SerializerFeature.DisableCircularReferenceDetect);
            System.out.println(s);
            return s;

        }else {
            String s = JSON.toJSONString(new ResponseBean(401,"SUCCESS",staffinfoList), SerializerFeature.DisableCircularReferenceDetect);
            System.out.println(s);
            return s;
        }


//
//        for (Staffinfo staffinfo: staffinfoList) {
//            System.out.println(staffinfo+"*************************");
//        }
//        if(null!=staffinfoList) {
//            ValueOperations operations = redisTemplate.opsForValue();
//            if(redisTemplate.hasKey("")){
//        }

    }


    // 修改用户
    @RequestMapping(value = "/update", method = {RequestMethod.PUT})

    public String updateStaffinfo(@RequestBody Dto dto) {
        System.out.println(dto);
        dto.getStaffinfo().getStaname().trim();
        dto.getStaffinfo().getStasex().trim();
        dto.getStaffinfo().getStaaddress().trim();
        Staffinfo staffinfo=dto.getStaffinfo();
        Position position=dto.getPosition();
        Department department=dto.getDepartment();
        boolean bool = iStaffinfoService.UpdateStaffinfo(staffinfo, position, department);
        if(bool){
            String s = JSON.toJSONString(new ResponseBean(200,"SUCCESS"));
            System.out.println(s);
            return s;
        }else {
            String s = JSON.toJSONString(new ResponseBean(401,"SUCCESS"));
            System.out.println(s);
            return s;
        }

    }


    //删除用户
    @RequestMapping(value = "/delect/{staid}", method = {RequestMethod.GET})

    public String updateStanstate(HttpServletRequest request,@PathVariable("staid") Integer staid) {
        System.out.println(request.getRequestURL());
        boolean bol = iStaffinfoService.DeleteStaffinfo(staid);
        if(bol){
            String s = JSON.toJSONString(new ResponseBean(200,"SUCCESS"));
            System.out.println(s);
            return s;
        } else {
            String s = JSON.toJSONString(new ResponseBean(401,"SUCCESS"));
            System.out.println(s);
            return s;
        }
    }


    //添加员工
    @RequestMapping(value = "/add", method = RequestMethod.POST)

    public String addStaffinfo(@RequestBody Dto dto) {

        System.out.println(dto);
        dto.getStaffinfo().getStaname().trim();
        dto.getStaffinfo().getStasex().trim();
        dto.getStaffinfo().getStaaddress().trim();
        dto.getStaffinfo().setStadbsource("hospital");
        dto.getStaffinfo().setStanstate("在职");
        Staffinfo staffinfo=dto.getStaffinfo();
        Position position=dto.getPosition();
        Department department=dto.getDepartment();
        //密码加密

        String md5Password = DigestUtils.md5DigestAsHex(staffinfo.getStapassword().getBytes());
        staffinfo.setStapassword(md5Password);
        boolean bol = iStaffinfoService.addStaffinfo(staffinfo, position,department );
        if(bol){
            return  JSON.toJSONString(new ResponseBean(200,"SUCCESS"));
        } else {

            return JSON.toJSONString(new ResponseBean(401,"FAIL"));
        }
    }


    //多条件搜索员工信息

  @RequestMapping(value = "/search" ,method = RequestMethod.GET)
    public String SearchMessage(@RequestBody Staffinfo staffinfo) {
      System.out.println(staffinfo);

        List <Staffinfo> list=iStaffinfoService.SelectStaffinfoBystajobnumberOrStaname(staffinfo);
        if(null!=list){
            String s = JSON.toJSONString(new ResponseBean(200,"SUCCESS",list));
            System.out.println(s);
            return s;

        }
       String s = JSON.toJSONString(new ResponseBean(401,"FAIL"));
       System.out.println(s);
       return s;
    }


}


//    @RequestMapping(value = "testzuul")
//    @ResponseBody
//    public  String testZuul(){
//        System.out.println("test zuul");
//
//        return  "test zuul";
//
//    }

